﻿using ProjectCommonLibs.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCommonLibs.utils
{
    public class CloudUtils
    {
        private readonly string remoteAddress;
        public CloudUtils(string remoteAddress)
        {
            this.remoteAddress = remoteAddress;
        }

        private HttpClient client = new HttpClient();

        public void insertData(object model)
        {
            try
            {
                var currentModel = model as HttpRequestModel;
                var tableName = currentModel.tableName;
                var time = currentModel.time;
                var tableData = currentModel.data;
                if (String.IsNullOrEmpty(tableName) || String.IsNullOrEmpty(time) || String.IsNullOrEmpty(tableData))
                {
                    return;
                }
                var form = new FormUrlEncodedContent(new Dictionary<string, string>() {
                    {"tableName",tableName },
                    {"time",time },
                    {"data",tableData }
                });

                client.PostAsync(remoteAddress, form);
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}
